<style scoped>
.split {
    height: calc(100vh - 140px);
    background: #ffffff;
    border: 1px solid #dcdee2;
}

.split-pane {
    padding: 10px;
}

/deep/.ivu-table-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}

/deep/.ivu-table-fixed-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}
</style>
<template>
    <div class="split">
        <Split v-model="split1">
            <div slot="left" class="split-pane">
                <Tree :data="treeData" @on-select-change="changeTree"></Tree>
            </div>
            <div slot="right" class="split-pane">
                <Card style="margin-bottom: 10px">
                    <Form inline class="form-search">
                        <FormItem>
                            <Input v-model="keyword" placeholder="姓名/医保号" style="width: 200px" />
                        </FormItem>
                        <FormItem>
                            <Button type="primary" @click="page = 1; SearchCustomer();" icon="md-search">查询</Button>
                        </FormItem>
                        <FormItem>
                            <Button type="primary" @click="AddData" icon="md-add">新增</Button>
                        </FormItem>
                        <FormItem>
                            <Button type="warning" @click="ResetCustomerBalance">重置余额</Button>
                        </FormItem>
                    </Form>
                </Card>
                <Table ref="table" style="margin-bottom: 10px" border :loading="loadingTable" :columns="tableTitle" :data="tableData" :height="tableHeight"></Table>
                <Page :total="total" :current="page" :page-size="size" :page-size-opts="[10, 50, 100]" @on-change="(s) => { page = s; SearchCustomer(); }" @on-page-size-change="(p) => { size = p; page = 1; SearchCustomer(); }" show-total show-sizer />
                <Modal :width="600" :footer-hide="true" v-model="modalData" :title="tempDataId > 0 ? '修改' : '新增'" :mask-closable="false" draggable sticky>
                    <customeredit :Id="tempDataId" v-if="modalData" @on-success="onSuccess"></customeredit>
                </Modal>
                <Modal :footer-hide="true" :width="1000" draggable v-model="modalLog" title="账目明细" :mask-closable="false" sticky>
                    <customerpaylog :Id="tempDataId" v-if="modalLog" />
                </Modal>
                <Modal :footer-hide="true" :width="600" draggable v-model="modalPay" title="向客户收款" :mask-closable="false" sticky>
                    <Customerpay :Id="tempDataId" v-if="modalPay" @on-success="onSuccess"></Customerpay>
                </Modal>
            </div>
        </Split>
    </div>
</template>

<script>

import { SearchCustomer, DeleteCustomer, ResetCustomerBalance } from '@/api/customer'
import { SearchDepartment, GetTreeDepartment } from '@/api/department'
import { InitDepartmentTree } from '../../libs/tools'
import customerpaylog from './customerpaylog.vue'
import customeredit from './customeredit.vue'
import Customerpay from './customerpay.vue'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
    name: 'customer',
    data() {
        return {
            split1: 0.15,
            Extras: {},
            total: 0,
            page: 1,
            size: 10,
            modalData: false,
            modalLog: false,
            modalPay: false,
            tableHeight: 450,
            keyword: '',
            ruleValidate: {
                Amount: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }
                ]

            },
            loading: false,
            loadingTable: false,
            tempData: {},
            tableTitle: [
                {
                    key: 'index',
                    type: 'index',
                    title: '序号',
                    align: 'center',
                    width: 50,
                    indexMethod: (params) => {
                        return params._index + (this.page - 1) * this.size + 1;
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    width: 150,
                    render: (h, params) => {
                        return h(
                            'div',
                            [h('Button', {
                                props: { type: 'primary', size: 'small' },
                                style: { marginRight: '5px' },
                                on: { click: () => { this.tempDataId = params.row.Id; this.modalData = true } }
                            }, '修改'),
                            h('Button', {
                                props: { type: 'error', size: 'small' },
                                style: { marginRight: '5px' },
                                on: { click: () => { this.DeleteCustomer(params.row.Id) } }
                            }, '删除')
                            ]
                        )
                    }
                },
                {
                    key: 'DisplayName',
                    title: '姓名',
                    width: 100,
                    align: 'center',
                },
                {
                    key: 'SexText',
                    title: '性别',
                    minWidth: 50,
                    align: 'center'
                }
                ,
                // {
                //     key: 'Age',
                //     title: '年龄',
                //     width: 120,
                //     align: 'center',
                // },
                // {
                //     key: 'Phone',
                //     title: '手机号',
                //     width: 120,
                //     align: 'center'

                // },
                {
                    key: 'DepartmentName',
                    title: '所属部门',
                    width: 200,
                    align: 'center'

                },
                {
                    key: 'Balance',
                    title: '账户余额(元)',
                    width: 150,
                    align: 'center',
                    render: (h, params) => {
                        return h(
                            'a',
                            {
                                style: {
                                    color: params.row.Balance >= 0 ? '#19be6b' : '#ed4014',
                                    fontSize: '18px',
                                    textDecoration: 'underline'
                                },
                                on: {
                                    click: () => {
                                        this.tempDataId = params.row.Id
                                        this.modalLog = true
                                    }
                                }
                            },
                            params.row.Balance
                        )
                    }
                },
                {
                    key: 'MedicalCardNo',
                    title: '医保号',
                    minWidth: 200,
                    align: 'center'
                }, {
                    key: 'Remark',
                    title: '备注',
                    minWidth: 200,
                    align: 'center'
                }
            ],
            typeid: 0,
            tableData: [],
            treeData: [],
            selectTreeData: [],
            tempDataId: null
        }
    },
    components: {
        customerpaylog,
        customeredit,
        Customerpay
    },
    computed: {},

    methods: {
        onSuccess(res) {
            this.modalData = false
            this.modalLog = false
            this.modalPay = false
            if (res == 1) {
                this.SearchCustomer()
            }
        },
        handleSummary({ columns, data }) {
            let vm = this

            const sums = {}
            columns.forEach((column, index) => {
                const key = column.key
                if (index === 0) {
                    sums[key] = {
                        key,
                        value: '合计'
                    }
                } else if (key == 'Balance') {
                    sums[key] = {
                        key,
                        value: Number(vm.Extras.Balance.toFixed(2))
                    }
                } else {
                    sums[key] = {
                        key,
                        value: ' '
                    }
                }
            })

            return sums
        },

        SearchCustomer() {
            let vm = this
            vm.loadingTable = true
            var reqData = {
                page: vm.page,
                size: vm.size,
                keyword: vm.keyword,
                typeid: vm.typeid
            }
            SearchCustomer(reqData).then((res) => {
                vm.loadingTable = false
                var data = res.data
                if (data.StatusCode == 200) {
                    vm.tableData = data.Data.Items
                    vm.total = data.Data.TotalCount
                    vm.Extras = data.Extras
                } else {
                    vm.$Notice.error({
                        title: data.Errors
                    })
                }
            })
        },
        DeleteCustomer(id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确定要删除吗？',
                onOk: () => {
                    DeleteCustomer(id).then((res) => {
                        const data = res.data
                        if (data.StatusCode == 200) {
                            this.SearchCustomer()
                            this.$Message.success({ title: '提示', content: '删除成功' })
                        } else {
                            this.$Message.error({ title: '提示', content: data.Errors })
                        }
                    })
                },
                onCancel: () => { }
            })
        },
        AddCustomerPay() {
            let vm = this
            vm.$refs['formValidate'].validate((valid) => {
                if (valid) {
                    vm.loading = true
                    AddCustomerPay(vm.tempData).then((res) => {
                        vm.loading = false
                        const data = res.data
                        if (data.StatusCode == 200) {
                            vm.modalData = false
                            vm.$Message.success({ title: '提示', content: '收款成功' })
                            vm.SearchCustomer()
                        } else {
                            vm.$Message.error({ title: '提示', content: data.Errors })
                        }
                    })
                }
            })
        },
        GetTreeDepartment() {
            let vm = this
            GetTreeDepartment(0).then((res) => {
                const data = res.data
                if (data.StatusCode == 200) {
                    this.selectTreeData = InitDepartmentTree(data.Data)
                    this.treeData = [
                        {
                            id: 0,
                            label: '全部',
                            title: '全部',
                            value: 0,
                            expand: true,
                            children: this.selectTreeData
                        }

                    ]
                } else {
                    vm.$Message.error({ title: '提示', content: data.Errors })
                }
            })
        },
        AddData() {
            this.tempDataId = 0
            this.modalData = true
        },
        changeTree(a, b) {
            this.typeid = b.id
            this.SearchCustomer()
        },
        ResetCustomerBalance() {
            this.$Modal.confirm({
                title: '提示',
                content: '确定要重置余额吗？',
                onOk: () => {
                    ResetCustomerBalance().then((res) => {
                        const data = res.data
                        if (data.StatusCode == 200) {
                            this.$Message.success({ title: '提示', content: '重置成功' })
                        } else {
                            this.$Message.error({ title: '提示', content: data.Errors })
                        }
                    })
                },
                onCancel: () => { }
            })
        },
    },
    mounted() {
        this.tableHeight = window.innerHeight - 300
        this.SearchCustomer()
        this.GetTreeDepartment()
    }
}
</script>

<style></style>
